package com.taotao.portal.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.CookieUtils;
import com.taotao.portal.pojo.CartItem;
import com.taotao.portal.service.CartService;

@RequestMapping("/cart")
@Controller
public class CartController {
	@Autowired
	private CartService cartService;

	/**
	 * 添加商品到购物车
	 * 
	 * @param itemId
	 * @param itemNum
	 * @param request
	 * @param response
	 * @return
	 * @author: JSJ
	 */
	@RequestMapping("/add/{itemId}")
	public String addCart(@PathVariable long itemId, Integer itemNum,
			HttpServletRequest request, HttpServletResponse response) {
		String token = CookieUtils.getCookieValue(request, "TT_TOKEN", true);
		TaotaoResult result = null;
		if (StringUtils.isNotBlank(token)) {
			result = cartService.addCartToRedis(itemId, itemNum, request,
					response);
		} else {
			result = cartService.addCart(itemId, itemNum, request, response);

		}
		return "cartSuccess";
	}

	/**
	 * 跳转到购物车列表页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 * @author: JSJ
	 */
	@RequestMapping("/cart")
	public String getCartItemList(HttpServletRequest request, Model model) {
		String token = CookieUtils.getCookieValue(request, "TT_TOKEN", true);
		List<CartItem> cartItemList = null;
		if (StringUtils.isNotBlank(token)) {
			cartItemList = cartService.getCartItemListByRedis(request);
		} else {
			cartItemList = cartService.getCartItemList(request);
		}
		model.addAttribute("cartList", cartItemList);
		return "cart";
	}

	/**
	 * 更新购物车商品数量
	 * 
	 * @param itemId
	 * @param num
	 * @param request
	 * @param response
	 * @return
	 * @author: JSJ
	 */
	@RequestMapping("/update/num/{itemId}/{num}")
	@ResponseBody
	public TaotaoResult updateCartItemNum(@PathVariable long itemId,
			@PathVariable int num, HttpServletRequest request,
			HttpServletResponse response) {
		String token = CookieUtils.getCookieValue(request, "TT_TOKEN", true);
		TaotaoResult result = null;
		if (StringUtils.isNotBlank(token)) {
			result = cartService.updateRedisCartItemNum(itemId, num, request,
					response);
		} else {
			result = cartService.updateCartItemNum(itemId, num, request,
					response);
		}
		return result;
	}

	/**
	 * 删除购物车中对应商品
	 * 
	 * @param itemId
	 * @param request
	 * @param response
	 * @return
	 * @author: JSJ
	 */
	@RequestMapping("/delete/{itemId}")
	public String deleteCartItemById(@PathVariable long itemId,
			HttpServletRequest request, HttpServletResponse response) {
		String token = CookieUtils.getCookieValue(request, "TT_TOKEN", true);
		TaotaoResult result = null;
		if (StringUtils.isNotBlank(token)) {
			result = cartService.deleteRedisCartItem(itemId, request, response);
		} else {
			result = cartService.deleteCartItem(itemId, request, response);
		}
		return "redirect:/cart/cart.html";
	}
}
